package com.issoft.studentresitor.controller.student;

import cn.hutool.core.bean.BeanUtil;
import com.issoft.studentresitor.domain.pojo.Dorm;
import com.issoft.studentresitor.domain.pojo.Result;
import com.issoft.studentresitor.domain.vo.DormVo;
import com.issoft.studentresitor.service.IDormService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "学生宿舍内容接口")
@Slf4j
@RestController
@RequestMapping("/student/drom")
public class StuDormController {
    @Autowired
    private IDormService dormService;
    @ApiOperation("根据当前的user_id查询自己的宿管信息")
    @GetMapping("/queryDormInfo/{userId}")
    public Result queryDormInfo(@PathVariable Integer userId) {
        System.out.println("查询的user_id为：" + userId);
        log.info("查询的user_id为：" + userId);
        Dorm dorm=dormService.queryDormInfo(userId);
        log.info("查询的宿舍信息为：{}", dorm);
        DormVo dormVo=new DormVo();
        BeanUtil.copyProperties(dorm, dormVo);
        log.info("查询的宿舍信息为：{}", dormVo);
        return Result.success(dormVo);
    }
    @ApiOperation("没有查到自己的宿舍信息查询所有宿舍")
    @GetMapping("/queryDorm")
    public Result queryDorm() {
        log.info("查询表里所有宿舍信息");
        List<Dorm> dorms = dormService.list();
        log.info("查询的宿舍信息为：{}", dorms);
        //po转vo
        List<DormVo> dormVos = dorms.stream().map(dorm -> {
            DormVo dormVo = new DormVo();
            BeanUtil.copyProperties(dorm, dormVo);
            return dormVo; //需要返回dormVo
        }).collect(Collectors.toList()); // 需要收集结果
        return Result.success(dormVos);
    }
    @ApiOperation("选择宿舍")
    @PutMapping("/{userId}/{dormId}")
    public Result selectDorm(@PathVariable Integer userId, @PathVariable Integer dormId) {
        log.info("选择宿舍的user_id为：{}", userId);
        log.info("选择宿舍的dorm_id为：{}", dormId);
        boolean result = dormService.selectDorm(userId, dormId);
        return result ? Result.success() : Result.error("选择宿舍失败");
    }
}
